/*
 * SystoleView.java
 */
package systole.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.io.IOException;
import java.net.URISyntaxException;

import java.util.EventObject;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTabbedPane;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import org.jdesktop.application.Application.ExitListener;
import systole.utils.ImageUtils;
import systole.view.tabs.JpnlAnalysis;

/**
 * The application's main frame.
 */
public class SystoleView extends FrameView {

    private MainController controller;
    private Image logoImage = ImageUtils.buildImage(this, "resources/systole_96.png");

    /**
     * @param app
     */
    public SystoleView(SingleFrameApplication app) {
        super(app);

        this.getFrame().setTitle("Systole - Sistema de Gestión de Curvas de Variabilidad de Distensión Arterial");

        this.getFrame().setIconImage(logoImage);

        try {
            UIManager.setLookAndFeel("javax.swing.plaf.basic.BasicLookAndFeel");

        } catch (Exception e) {
            //
        }

        initComponents();

        this.controller = new MainController(this);

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);
        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });

        app.addExitListener(new SystoleWindowsAdapter(this.controller));
        this.getFrame().setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    /**
     * Show about form
     */
    @Action
    public void showAboutBox() {
        JDialog aboutBox;
        JFrame mainFrame = SystoleApp.getApplication().getMainFrame();
        aboutBox = new SystoleAboutBox(mainFrame);
        aboutBox.setLocationRelativeTo(mainFrame);
        aboutBox.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jTabPnlMain = new javax.swing.JTabbedPane();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMItemNewAnalysis = new javax.swing.JMenuItem();
        jMItemOpenAnalysis = new javax.swing.JMenuItem();
        jMItemSaveAnalysis = new javax.swing.JMenuItem();
        jMItemSaveAll = new javax.swing.JMenuItem();
        jMItemCloseAnalysis = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMtemPreview = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        entitiesMenu = new javax.swing.JMenu();
        signalFrequencyMenuItem = new javax.swing.JMenuItem();
        patientMenuItem = new javax.swing.JMenuItem();
        medicMenuItem = new javax.swing.JMenuItem();
        familyMenuItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        medicineMenuItem = new javax.swing.JMenuItem();
        pathologyMenuItem = new javax.swing.JMenuItem();
        surgeryMenuItem = new javax.swing.JMenuItem();
        sportMenuItem = new javax.swing.JMenuItem();
        professionMenuItem = new javax.swing.JMenuItem();
        standardValuesMenuItem = new javax.swing.JMenuItem();
        reportMenu = new javax.swing.JMenu();
        commentMenuItem = new javax.swing.JMenuItem();
        conclusionMenuItem = new javax.swing.JMenuItem();
        comparativesMenuItem = new javax.swing.JMenuItem();
        averagesMenuItem = new javax.swing.JMenuItem();
        settingsMenu = new javax.swing.JMenu();
        settingsMenuItem = new javax.swing.JMenuItem();
        reportSettingsMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        helpMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        jToolBarMain = new javax.swing.JToolBar();
        jBtnNewAnalysis = new javax.swing.JButton();
        jBtnOpenAnalysis = new javax.swing.JButton();
        jBtnSaveAnalysis = new javax.swing.JButton();
        jBtnSaveAll = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        jBtnPrint = new javax.swing.JButton();
        jBtnToPdf = new javax.swing.JButton();
        jBtnPreview = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        jButton1 = new javax.swing.JButton();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(400, 400));
        mainPanel.setLayout(new java.awt.BorderLayout());

        jTabPnlMain.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
        jTabPnlMain.setName("jTabPnlMain"); // NOI18N
        mainPanel.add(jTabPnlMain, java.awt.BorderLayout.CENTER);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getResourceMap(SystoleView.class);
        jTabPnlMain.getAccessibleContext().setAccessibleName(resourceMap.getString("jTabPnlMain.AccessibleContext.accessibleName")); // NOI18N

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setIcon(resourceMap.getIcon("fileMenu.icon")); // NOI18N
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getActionMap(SystoleView.class, this);
        jMItemNewAnalysis.setAction(actionMap.get("newAnalysis")); // NOI18N
        jMItemNewAnalysis.setIcon(resourceMap.getIcon("jMItemNewAnalysis.icon")); // NOI18N
        jMItemNewAnalysis.setText(resourceMap.getString("jMItemNewAnalysis.text")); // NOI18N
        jMItemNewAnalysis.setName("jMItemNewAnalysis"); // NOI18N
        fileMenu.add(jMItemNewAnalysis);

        jMItemOpenAnalysis.setAction(actionMap.get("openAnalysis")); // NOI18N
        jMItemOpenAnalysis.setIcon(resourceMap.getIcon("jMItemOpenAnalysis.icon")); // NOI18N
        jMItemOpenAnalysis.setText(resourceMap.getString("jMItemOpenAnalysis.text")); // NOI18N
        jMItemOpenAnalysis.setToolTipText(resourceMap.getString("jMItemOpenAnalysis.toolTipText")); // NOI18N
        jMItemOpenAnalysis.setName("jMItemOpenAnalysis"); // NOI18N
        fileMenu.add(jMItemOpenAnalysis);

        jMItemSaveAnalysis.setAction(actionMap.get("save")); // NOI18N
        jMItemSaveAnalysis.setIcon(resourceMap.getIcon("jMItemSaveAnalysis.icon")); // NOI18N
        jMItemSaveAnalysis.setText(resourceMap.getString("jMItemSaveAnalysis.text")); // NOI18N
        jMItemSaveAnalysis.setToolTipText(resourceMap.getString("jMItemSaveAnalysis.toolTipText")); // NOI18N
        jMItemSaveAnalysis.setName("jMItemSaveAnalysis"); // NOI18N
        fileMenu.add(jMItemSaveAnalysis);

        jMItemSaveAll.setAction(actionMap.get("saveAllAnalysis")); // NOI18N
        jMItemSaveAll.setIcon(resourceMap.getIcon("jMItemSaveAll.icon")); // NOI18N
        jMItemSaveAll.setText(resourceMap.getString("jMItemSaveAll.text")); // NOI18N
        jMItemSaveAll.setToolTipText(resourceMap.getString("jMItemSaveAll.toolTipText")); // NOI18N
        jMItemSaveAll.setName("jMItemSaveAll"); // NOI18N
        fileMenu.add(jMItemSaveAll);

        jMItemCloseAnalysis.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.CTRL_MASK));
        jMItemCloseAnalysis.setIcon(resourceMap.getIcon("jMItemCloseAnalysis.icon")); // NOI18N
        jMItemCloseAnalysis.setText(resourceMap.getString("jMItemCloseAnalysis.text")); // NOI18N
        jMItemCloseAnalysis.setToolTipText(resourceMap.getString("jMItemCloseAnalysis.toolTipText")); // NOI18N
        jMItemCloseAnalysis.setName("jMItemCloseAnalysis"); // NOI18N
        fileMenu.add(jMItemCloseAnalysis);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fileMenu.add(jSeparator1);

        jMtemPreview.setAction(actionMap.get("preview")); // NOI18N
        jMtemPreview.setIcon(resourceMap.getIcon("jMtemPreview.icon")); // NOI18N
        jMtemPreview.setText(resourceMap.getString("jMtemPreview.text")); // NOI18N
        jMtemPreview.setName("jMtemPreview"); // NOI18N
        fileMenu.add(jMtemPreview);

        jSeparator3.setName("jSeparator3"); // NOI18N
        fileMenu.add(jSeparator3);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setIcon(resourceMap.getIcon("exitMenuItem.icon")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setToolTipText(resourceMap.getString("exitMenuItem.toolTipText")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        entitiesMenu.setText(resourceMap.getString("entitiesMenu.text")); // NOI18N

        signalFrequencyMenuItem.setAction(actionMap.get("signalFrequencyAdmin")); // NOI18N
        signalFrequencyMenuItem.setIcon(resourceMap.getIcon("signalFrequencyMenuItem.icon")); // NOI18N
        signalFrequencyMenuItem.setText(resourceMap.getString("signalFrequencyMenuItem.text")); // NOI18N
        signalFrequencyMenuItem.setName("signalFrequencyMenuItem"); // NOI18N
        entitiesMenu.add(signalFrequencyMenuItem);

        patientMenuItem.setAction(actionMap.get("adminPatients")); // NOI18N
        patientMenuItem.setIcon(resourceMap.getIcon("patientMenuItem.icon")); // NOI18N
        patientMenuItem.setText(resourceMap.getString("patientMenuItem.text")); // NOI18N
        patientMenuItem.setToolTipText(resourceMap.getString("patientMenuItem.toolTipText")); // NOI18N
        patientMenuItem.setName("patientMenuItem"); // NOI18N
        entitiesMenu.add(patientMenuItem);

        medicMenuItem.setAction(actionMap.get("adminMedics")); // NOI18N
        medicMenuItem.setIcon(resourceMap.getIcon("medicMenuItem.icon")); // NOI18N
        medicMenuItem.setText(resourceMap.getString("medicMenuItem.text")); // NOI18N
        medicMenuItem.setName("medicMenuItem"); // NOI18N
        entitiesMenu.add(medicMenuItem);

        familyMenuItem.setAction(actionMap.get("familyAdmin")); // NOI18N
        familyMenuItem.setIcon(resourceMap.getIcon("familyMenuItem.icon")); // NOI18N
        familyMenuItem.setText(resourceMap.getString("familyMenuItem.text")); // NOI18N
        familyMenuItem.setToolTipText(resourceMap.getString("familyMenuItem.toolTipText")); // NOI18N
        familyMenuItem.setName("familyMenuItem"); // NOI18N
        entitiesMenu.add(familyMenuItem);

        jSeparator2.setName("jSeparator2"); // NOI18N
        entitiesMenu.add(jSeparator2);

        medicineMenuItem.setAction(actionMap.get("adminMedicines")); // NOI18N
        medicineMenuItem.setIcon(resourceMap.getIcon("medicineMenuItem.icon")); // NOI18N
        medicineMenuItem.setText(resourceMap.getString("medicineMenuItem.text")); // NOI18N
        medicineMenuItem.setName("medicineMenuItem"); // NOI18N
        entitiesMenu.add(medicineMenuItem);

        pathologyMenuItem.setAction(actionMap.get("pathologyAdmin")); // NOI18N
        pathologyMenuItem.setIcon(resourceMap.getIcon("pathologyMenuItem.icon")); // NOI18N
        pathologyMenuItem.setText(resourceMap.getString("pathologyMenuItem.text")); // NOI18N
        pathologyMenuItem.setName("pathologyMenuItem"); // NOI18N
        entitiesMenu.add(pathologyMenuItem);

        surgeryMenuItem.setAction(actionMap.get("surgeryAdmin")); // NOI18N
        surgeryMenuItem.setIcon(resourceMap.getIcon("surgeryMenuItem.icon")); // NOI18N
        surgeryMenuItem.setText(resourceMap.getString("surgeryMenuItem.text")); // NOI18N
        surgeryMenuItem.setName("surgeryMenuItem"); // NOI18N
        entitiesMenu.add(surgeryMenuItem);

        sportMenuItem.setAction(actionMap.get("sportAdmin")); // NOI18N
        sportMenuItem.setIcon(resourceMap.getIcon("sportMenuItem.icon")); // NOI18N
        sportMenuItem.setText(resourceMap.getString("sportMenuItem.text")); // NOI18N
        sportMenuItem.setName("sportMenuItem"); // NOI18N
        entitiesMenu.add(sportMenuItem);

        professionMenuItem.setAction(actionMap.get("professionAdmin")); // NOI18N
        professionMenuItem.setIcon(resourceMap.getIcon("professionMenuItem.icon")); // NOI18N
        professionMenuItem.setText(resourceMap.getString("professionMenuItem.text")); // NOI18N
        professionMenuItem.setToolTipText(resourceMap.getString("professionMenuItem.toolTipText")); // NOI18N
        professionMenuItem.setName("professionMenuItem"); // NOI18N
        entitiesMenu.add(professionMenuItem);

        standardValuesMenuItem.setAction(actionMap.get("adminStandardValues")); // NOI18N
        standardValuesMenuItem.setIcon(resourceMap.getIcon("standardValuesMenuItem.icon")); // NOI18N
        standardValuesMenuItem.setText(resourceMap.getString("standardValuesMenuItem.text")); // NOI18N
        standardValuesMenuItem.setToolTipText(resourceMap.getString("standardValuesMenuItem.toolTipText")); // NOI18N
        standardValuesMenuItem.setName("standardValuesMenuItem"); // NOI18N
        entitiesMenu.add(standardValuesMenuItem);

        menuBar.add(entitiesMenu);

        reportMenu.setText(resourceMap.getString("reportMenu.text")); // NOI18N
        reportMenu.setName("reportMenu"); // NOI18N

        commentMenuItem.setAction(actionMap.get("commentAdmin")); // NOI18N
        commentMenuItem.setIcon(resourceMap.getIcon("commentMenuItem.icon")); // NOI18N
        commentMenuItem.setText(resourceMap.getString("commentMenuItem.text")); // NOI18N
        commentMenuItem.setToolTipText(resourceMap.getString("commentMenuItem.toolTipText")); // NOI18N
        commentMenuItem.setName("commentMenuItem"); // NOI18N
        reportMenu.add(commentMenuItem);

        conclusionMenuItem.setAction(actionMap.get("conclusionAdmin")); // NOI18N
        conclusionMenuItem.setIcon(resourceMap.getIcon("conclusionMenuItem.icon")); // NOI18N
        conclusionMenuItem.setText(resourceMap.getString("conclusionMenuItem.text")); // NOI18N
        conclusionMenuItem.setToolTipText(resourceMap.getString("conclusionMenuItem.toolTipText")); // NOI18N
        conclusionMenuItem.setName("conclusionMenuItem"); // NOI18N
        reportMenu.add(conclusionMenuItem);

        comparativesMenuItem.setAction(actionMap.get("adminComparative")); // NOI18N
        comparativesMenuItem.setIcon(resourceMap.getIcon("comparativesMenuItem.icon")); // NOI18N
        comparativesMenuItem.setText(resourceMap.getString("comparativesMenuItem.text")); // NOI18N
        comparativesMenuItem.setToolTipText(resourceMap.getString("comparativesMenuItem.toolTipText")); // NOI18N
        comparativesMenuItem.setName("comparativesMenuItem"); // NOI18N
        reportMenu.add(comparativesMenuItem);

        averagesMenuItem.setAction(actionMap.get("viewAverages")); // NOI18N
        averagesMenuItem.setIcon(resourceMap.getIcon("averagesMenuItem.icon")); // NOI18N
        averagesMenuItem.setText(resourceMap.getString("averagesMenuItem.text")); // NOI18N
        averagesMenuItem.setToolTipText(resourceMap.getString("averagesMenuItem.toolTipText")); // NOI18N
        averagesMenuItem.setName("averagesMenuItem"); // NOI18N
        reportMenu.add(averagesMenuItem);

        menuBar.add(reportMenu);

        settingsMenu.setText(resourceMap.getString("settingsMenu.text")); // NOI18N

        settingsMenuItem.setAction(actionMap.get("settings")); // NOI18N
        settingsMenuItem.setIcon(resourceMap.getIcon("settingsMenuItem.icon")); // NOI18N
        settingsMenuItem.setText(resourceMap.getString("settingsMenuItem.text")); // NOI18N
        settingsMenuItem.setName("settingsMenuItem"); // NOI18N
        settingsMenu.add(settingsMenuItem);

        reportSettingsMenuItem.setAction(actionMap.get("reportSettings")); // NOI18N
        reportSettingsMenuItem.setIcon(resourceMap.getIcon("reportSettingsMenuItem.icon")); // NOI18N
        reportSettingsMenuItem.setText(resourceMap.getString("reportSettingsMenuItem.text")); // NOI18N
        reportSettingsMenuItem.setName("reportSettingsMenuItem"); // NOI18N
        settingsMenu.add(reportSettingsMenuItem);

        menuBar.add(settingsMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setIcon(resourceMap.getIcon("aboutMenuItem.icon")); // NOI18N
        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setToolTipText(resourceMap.getString("aboutMenuItem.toolTipText")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        helpMenuItem.setAction(actionMap.get("help")); // NOI18N
        helpMenuItem.setIcon(resourceMap.getIcon("helpMenuItem.icon")); // NOI18N
        helpMenuItem.setText(resourceMap.getString("helpMenuItem.text")); // NOI18N
        helpMenuItem.setName("helpMenuItem"); // NOI18N
        helpMenu.add(helpMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 578, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        jToolBarMain.setFloatable(false);
        jToolBarMain.setName("jToolBarMain"); // NOI18N
        jToolBarMain.setPreferredSize(new java.awt.Dimension(172, 30));

        jBtnNewAnalysis.setAction(actionMap.get("newAnalysis")); // NOI18N
        jBtnNewAnalysis.setIcon(resourceMap.getIcon("jBtnNewAnalysis.icon")); // NOI18N
        jBtnNewAnalysis.setToolTipText(resourceMap.getString("jBtnNewAnalysis.toolTipText")); // NOI18N
        jBtnNewAnalysis.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jBtnNewAnalysis.setBorderPainted(false);
        jBtnNewAnalysis.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jBtnNewAnalysis.setName("jBtnNewAnalysis"); // NOI18N
        jBtnNewAnalysis.setPreferredSize(new java.awt.Dimension(30, 30));
        jBtnNewAnalysis.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(jBtnNewAnalysis);

        jBtnOpenAnalysis.setAction(actionMap.get("openAnalysis")); // NOI18N
        jBtnOpenAnalysis.setIcon(resourceMap.getIcon("jBtnOpenAnalysis.icon")); // NOI18N
        jBtnOpenAnalysis.setText(resourceMap.getString("jBtnOpenAnalysis.text")); // NOI18N
        jBtnOpenAnalysis.setToolTipText(resourceMap.getString("jBtnOpenAnalysis.toolTipText")); // NOI18N
        jBtnOpenAnalysis.setFocusable(false);
        jBtnOpenAnalysis.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jBtnOpenAnalysis.setMaximumSize(new java.awt.Dimension(31, 31));
        jBtnOpenAnalysis.setName("jBtnOpenAnalysis"); // NOI18N
        jBtnOpenAnalysis.setPreferredSize(new java.awt.Dimension(30, 30));
        jBtnOpenAnalysis.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(jBtnOpenAnalysis);

        jBtnSaveAnalysis.setAction(actionMap.get("save")); // NOI18N
        jBtnSaveAnalysis.setIcon(resourceMap.getIcon("jBtnSaveAnalysis.icon")); // NOI18N
        jBtnSaveAnalysis.setText(resourceMap.getString("jBtnSaveAnalysis.text")); // NOI18N
        jBtnSaveAnalysis.setToolTipText(resourceMap.getString("jBtnSaveAnalysis.toolTipText")); // NOI18N
        jBtnSaveAnalysis.setFocusable(false);
        jBtnSaveAnalysis.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jBtnSaveAnalysis.setName("jBtnSaveAnalysis"); // NOI18N
        jBtnSaveAnalysis.setPreferredSize(new java.awt.Dimension(30, 30));
        jBtnSaveAnalysis.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(jBtnSaveAnalysis);

        jBtnSaveAll.setIcon(resourceMap.getIcon("jBtnSaveAll.icon")); // NOI18N
        jBtnSaveAll.setText(resourceMap.getString("jBtnSaveAll.text")); // NOI18N
        jBtnSaveAll.setToolTipText(resourceMap.getString("jBtnSaveAll.toolTipText")); // NOI18N
        jBtnSaveAll.setFocusable(false);
        jBtnSaveAll.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jBtnSaveAll.setName("jBtnSaveAll"); // NOI18N
        jBtnSaveAll.setPreferredSize(new java.awt.Dimension(30, 30));
        jBtnSaveAll.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(jBtnSaveAll);

        jSeparator4.setName("jSeparator4"); // NOI18N
        jToolBarMain.add(jSeparator4);

        jBtnPrint.setAction(actionMap.get("printReport")); // NOI18N
        jBtnPrint.setIcon(resourceMap.getIcon("jBtnPrint.icon")); // NOI18N
        jBtnPrint.setText(resourceMap.getString("jBtnPrint.text")); // NOI18N
        jBtnPrint.setToolTipText(resourceMap.getString("jBtnPrint.toolTipText")); // NOI18N
        jBtnPrint.setFocusable(false);
        jBtnPrint.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jBtnPrint.setName("jBtnPrint"); // NOI18N
        jBtnPrint.setPreferredSize(new java.awt.Dimension(30, 30));
        jBtnPrint.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(jBtnPrint);

        jBtnToPdf.setAction(actionMap.get("exportReport")); // NOI18N
        jBtnToPdf.setIcon(resourceMap.getIcon("jBtnToPdf.icon")); // NOI18N
        jBtnToPdf.setText(resourceMap.getString("jBtnToPdf.text")); // NOI18N
        jBtnToPdf.setFocusable(false);
        jBtnToPdf.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jBtnToPdf.setName("jBtnToPdf"); // NOI18N
        jBtnToPdf.setPreferredSize(new java.awt.Dimension(30, 30));
        jBtnToPdf.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(jBtnToPdf);

        jBtnPreview.setAction(actionMap.get("previewReport")); // NOI18N
        jBtnPreview.setIcon(resourceMap.getIcon("jBtnPreview.icon")); // NOI18N
        jBtnPreview.setText(resourceMap.getString("jBtnPreview.text")); // NOI18N
        jBtnPreview.setToolTipText(resourceMap.getString("jBtnPreview.toolTipText")); // NOI18N
        jBtnPreview.setFocusable(false);
        jBtnPreview.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jBtnPreview.setName("jBtnPreview"); // NOI18N
        jBtnPreview.setPreferredSize(new java.awt.Dimension(30, 30));
        jBtnPreview.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(jBtnPreview);

        jSeparator5.setName("jSeparator5"); // NOI18N
        jToolBarMain.add(jSeparator5);

        jButton1.setAction(actionMap.get("synchronizationSystole")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setMaximumSize(new java.awt.Dimension(30, 30));
        jButton1.setMinimumSize(new java.awt.Dimension(30, 30));
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setPreferredSize(new java.awt.Dimension(30, 30));
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(jButton1);

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
        setToolBar(jToolBarMain);
    }// </editor-fold>//GEN-END:initComponents

    /**
     *
     */
    @Action
    public void newAnalysis() {
        this.controller.newAnalysis();
    }

    /**
     * Action to CRUD patients 
     */
    @Action
    public void adminPatients() {
        this.controller.adminPatients();
    }

    /**
     * Action to CRUD Medics 
     */
    @Action
    public void adminMedics() {
        this.controller.adminMedics();
    }

    /**
     * Action to CRUD medicines 
     */
    @Action
    public void adminMedicines() {
        this.controller.adminMedicines();
    }

    /**
     * Action to CRUD pathologies
     */
    @Action
    public void pathologyAdmin() {
        this.controller.adminPathologies();
    }

    /**
     * Action to CRUD surgeries
     */
    @Action
    public void surgeryAdmin() {
        this.controller.adminSurgeries();
    }

    /**
     * Action to CRUD sports
     */
    @Action
    public void sportAdmin() {
        this.controller.adminSports();
    }

    /**
     * Action to CRUD frequency
     */
    @Action
    public void signalFrequencyAdmin() {
        this.controller.adminSamplingPeriod();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem averagesMenuItem;
    private javax.swing.JMenuItem commentMenuItem;
    private javax.swing.JMenuItem comparativesMenuItem;
    private javax.swing.JMenuItem conclusionMenuItem;
    private javax.swing.JMenu entitiesMenu;
    private javax.swing.JMenuItem familyMenuItem;
    private javax.swing.JMenuItem helpMenuItem;
    private javax.swing.JButton jBtnNewAnalysis;
    private javax.swing.JButton jBtnOpenAnalysis;
    private javax.swing.JButton jBtnPreview;
    private javax.swing.JButton jBtnPrint;
    private javax.swing.JButton jBtnSaveAll;
    private javax.swing.JButton jBtnSaveAnalysis;
    private javax.swing.JButton jBtnToPdf;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenuItem jMItemCloseAnalysis;
    private javax.swing.JMenuItem jMItemNewAnalysis;
    private javax.swing.JMenuItem jMItemOpenAnalysis;
    private javax.swing.JMenuItem jMItemSaveAll;
    private javax.swing.JMenuItem jMItemSaveAnalysis;
    private javax.swing.JMenuItem jMtemPreview;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JTabbedPane jTabPnlMain;
    private javax.swing.JToolBar jToolBarMain;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuItem medicMenuItem;
    private javax.swing.JMenuItem medicineMenuItem;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem pathologyMenuItem;
    private javax.swing.JMenuItem patientMenuItem;
    private javax.swing.JMenuItem professionMenuItem;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JMenu reportMenu;
    private javax.swing.JMenuItem reportSettingsMenuItem;
    private javax.swing.JMenu settingsMenu;
    private javax.swing.JMenuItem settingsMenuItem;
    private javax.swing.JMenuItem signalFrequencyMenuItem;
    private javax.swing.JMenuItem sportMenuItem;
    private javax.swing.JMenuItem standardValuesMenuItem;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JMenuItem surgeryMenuItem;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    /**
     * @return Main tab panel
     */
    public JTabbedPane getjTabPnlMain() {
        return jTabPnlMain;
    }

    /**
     * @param frmAnalysis
     */
    public void addAnalysisPanel(final JpnlAnalysis frmAnalysis) {

        JPanel tab = new JPanel();
        tab.setOpaque(false);

        JLabel tabLabel = new JLabel(frmAnalysis.getAnalysis().getPatient().getFullName());
        java.net.URL imageURL = SystoleView.class.getResource("resources/icons/cancel/cancel16.png");
        ImageIcon closeXIcon = new ImageIcon(imageURL);

        JButton tabCloseButton = new JButton(closeXIcon);
        tabCloseButton.setPreferredSize(new Dimension(closeXIcon.getIconWidth() + 2, closeXIcon.getIconHeight() + 2));
        tabCloseButton.setBorderPainted(false);
        tabCloseButton.setToolTipText("Cerrar Análisis");
        tabCloseButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                controller.closeAnalysis(frmAnalysis);
            }
        });

        tab.add(tabLabel, BorderLayout.WEST);
        tab.add(tabCloseButton, BorderLayout.EAST);


        this.getjTabPnlMain().addTab(null, frmAnalysis);
        this.getjTabPnlMain().setTabComponentAt(getjTabPnlMain().getTabCount() - 1, tab);
        this.getjTabPnlMain().setSelectedIndex(getjTabPnlMain().getTabCount() - 1);

    }

    @Action
    public void save() {
        this.controller.saveAnalysis();
    }

    @Action
    public void commentAdmin() {
        this.controller.adminComments();
    }

    @Action
    public void conclusionAdmin() {
        this.controller.adminConclusions();
    }

    @Action
    public void professionAdmin() {
        this.controller.adminProfessions();
    }

    @Action
    public void viewAverages() {
        this.controller.showAverages();
    }

    @Action
    public void familyAdmin() {
        this.controller.adminFamily();
    }

    @Action
    public void help() {
        if (java.awt.Desktop.isDesktopSupported()) {
            java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
            if (desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
                try {
                    String url = System.getProperty("user.dir") + System.getProperty("file.separator") + "help_file_html" + System.getProperty("file.separator") + "index.htm";
                    File file = new File(url);
                    desktop.open(file);
                } catch (IOException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }
    }

    @Action
    public void saveAllAnalysis() {
        this.controller.saveAllAnalysis();
    }

    @Action
    public void openAnalysis() {
        this.controller.openAnalysis();
    }

    private class SystoleWindowsAdapter implements ExitListener {

        private MainController mainController;

        public SystoleWindowsAdapter(MainController mainController) {
            this.mainController = mainController;
        }

        public boolean canExit(EventObject eo) {
            return mainController.closeApplication();
        }

        public void willExit(EventObject eo) {
        }
    }

    @Action
    public void reportSettings() {
        this.controller.reportSettings();
    }

    @Action
    public void settings() {
        this.controller.settings();
    }

    @Action
    public void preview() {
        this.controller.preview();
    }

    @Action
    public void adminComparative() {
        this.controller.adminComparatives();
    }

    @Action
    public void printReport() {
        this.controller.printReport();
    }

    @Action
    public void exportReport() {
        this.controller.exportReport();
    }

    @Action
    public void previewReport() {
        this.controller.previewReport();
    }

    @Action
    public void adminStandardValues() {
        this.controller.adminStandardValues();
    }

    @Action
    public void synchronizationSystole() {
        this.controller.syncInfo();
    }
}
